package com.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.entity.Blog;
import com.manage.entity.Book;
import com.manage.entity.Pickup;
import com.manage.entity.vo.PieEchartsRes;
import com.manage.mapper.BlogMapper;
import com.manage.mapper.BookMapper;
import com.manage.mapper.PickupMapper;
import com.manage.service.IBlogService;
import com.manage.service.IPickupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *

 */
@Service
public class PickupServiceImpl extends ServiceImpl<PickupMapper, Pickup> implements IPickupService {

    @Autowired
    private PickupMapper pickupMapper;
    @Autowired
    private BookMapper bookMapper;
    /**
     * 分类查询领取记录列表
     * @param current
     * @param size
     * @return
     */
    @Override
    public Page<Pickup> selectListByPage(Integer current, Integer size) {
        current=current==null?1:current;
        size=size==null?5:size;
        Page<Pickup> page = new Page(current,size);
        Page<Pickup> pageList = pickupMapper.selectPage(page, new QueryWrapper<Pickup>().orderByDesc("create_time"));
        List<Pickup> records = pageList.getRecords();
        List<Pickup> pickupList = records.stream().map(pickup -> {
            Integer bookId = pickup.getBookId();
            Book book = bookMapper.selectById(bookId);
            pickup.setBook(book);
            return pickup;
        }).collect(Collectors.toList());
        pageList.setRecords(pickupList);
        return pageList;
    }

    /**
     * 根据条件查询领取记录列表
     * @param pickup
     * @return
     */
    @Override
    public List<Pickup> queryByCondition(Pickup pickup) {
        QueryWrapper<Pickup> queryWrapper = new QueryWrapper<Pickup>().like(pickup.getPeople()!=null,"people", pickup.getPeople());
        List<Pickup> pickups = pickupMapper.selectList(queryWrapper);
        List<Pickup> list = pickups.stream().map(item -> {
            Integer bookId = item.getBookId();
            Book book = bookMapper.selectById(bookId);
            item.setBook(book);
            return item;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 新增领取记录
     * @param pickup
     */
    @Transactional
    @Override
    public void savePickup(Pickup pickup) {
        pickupMapper.insert(pickup);
        //扣减库存
        Book book = bookMapper.selectById(pickup.getBookId());
        Long count = pickup.getCount();
        long number = book.getInventory() - count;
        if(number<0){
            throw new RuntimeException("库存不足");
        }
        book.setInventory(number);
        bookMapper.updateById(book);
    }

    /**
     * 删除领取记录
     * @param id
     */
    @Transactional
    @Override
    public void deleteById(Integer id) {
        Pickup pickup = pickupMapper.selectById(id);
        //修改库存
        Book book = bookMapper.selectById(pickup.getBookId());
        Long count = pickup.getCount();
        book.setInventory(book.getInventory()+count);
        bookMapper.updateById(book);
        //删除领取记录
        pickupMapper.deleteById(id);
    }

    @Override
    public List<PieEchartsRes> countForEcharts() {
        List<PieEchartsRes> list = pickupMapper.countForEcharts();
        return list;

    }
}
